package com.gzhryc.jdbc;

import com.gzhryc.jdbc.enums.DateBranchTableType;
import com.gzhryc.jdbc.log.Logger;
import com.gzhryc.jdbc.utils.DateTools;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateBranchHelper {

	static Logger log = Logger.getLogger(DateBranchHelper.class);

	static DateBranchHelper me;

	public static DateBranchHelper self() {
		if (me == null) {
			me = new DateBranchHelper();
		}
		return me;
	}

	private Date defaultStartDate;
	private Date halfYearEndDate;        //当前半年数据的最后时间
	private Date quarterEndDate;        //当前季数据的最后时间
	private Date monthEndDate;            //当前月数据的最后时间
	private Date yearEndDate;            //当前年数据的最后时间
	private List<DateBranchItem> halfYearItems;
	private List<DateBranchItem> quarterItems;
	private List<DateBranchItem> monthItems;
	private List<DateBranchItem> yearItems;

	public Date getDefaultStartDate() {
		return defaultStartDate;
	}

	public void setDefaultStartDate(Date date) {
		this.defaultStartDate = date;
		this.halfYearItems = null;
		this.quarterItems = null;
		this.monthItems = null;
		this.yearItems = null;
	}

	public DateBranchHelper initial(String startDateStr) {
		try {
			setDefaultStartDate(DateUtils.parseDateStrictly(startDateStr, "yyyy-MM"));
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
		return this;
	}

	public List<DateBranchItem> getHalfYearDateBranchItems() {
		if (this.defaultStartDate != null) {
			Date currentDate = new Date();
			if (halfYearItems == null || !DateTools.isSameMonth(halfYearEndDate, currentDate)) {
				halfYearEndDate = currentDate;
				halfYearItems = getHalfYearDateBranchItems(this.defaultStartDate);
			}
		} else {
			log.error("未设置默认开始时间");
		}
		return halfYearItems;
	}

	public List<DateBranchItem> getQuarterDateBranchItems() {
		if (this.defaultStartDate != null) {
			Date currentDate = new Date();
			if (quarterItems == null || !DateTools.isSameMonth(quarterEndDate, currentDate)) {
				quarterEndDate = currentDate;
				quarterItems = getQuarterDateBranchItems(this.defaultStartDate);
			}
		} else {
			log.error("未设置默认开始时间");
		}
		return quarterItems;
	}

	public List<DateBranchItem> getMonthDateBranchItems() {
		if (this.defaultStartDate != null) {
			Date currentDate = new Date();
			if (monthItems == null || !DateTools.isSameMonth(monthEndDate, currentDate)) {
				monthEndDate = currentDate;
				monthItems = getMonthDateBranchItems(this.defaultStartDate);
			}
		} else {
			log.error("未设置默认开始时间");
		}
		return monthItems;
	}

	public List<DateBranchItem> getYearDateBranchItems() {
		if (this.defaultStartDate != null) {
			Date currentDate = new Date();
			if (yearItems == null || !DateTools.isSameYear(yearEndDate, currentDate)) {
				yearEndDate = currentDate;
				yearItems = getYearDateBranchItems(this.defaultStartDate);
			}
		} else {
			log.error("未设置默认开始时间");
		}
		return yearItems;
	}

	public static List<DateBranchItem> getHalfYearDateBranchItems(Date startDate) {
		List<DateBranchItem> result = new ArrayList<DateBranchItem>();
		Date endDate = new Date();
		Calendar cale = Calendar.getInstance();
		cale.setTime(endDate);
		int halfYear = cale.get(Calendar.MONTH) / 6 + 1;
		String year = DateFormatUtils.format(endDate, "yyyy");
		if (halfYear == 2) {
			result.add(new DateBranchItem(year + "07", year + "年7-12月"));
		}
		result.add(new DateBranchItem(year + "01", year + "年1-6月"));

		if (startDate != null) {
			int yearNum = DateTools.differenceYear(startDate, endDate);
			for (int i = 1; i <= yearNum; i++) {
				Date temp = DateUtils.addYears(endDate, -i);
				String prevYear = DateFormatUtils.format(temp, "yyyy");
				result.add(new DateBranchItem(prevYear + "07", prevYear + "年7-12月"));

				if (yearNum == 1) {
					cale = Calendar.getInstance();
					cale.setTime(startDate);
					if (cale.get(Calendar.MONTH) <= 6) {
						result.add(new DateBranchItem(prevYear + "01", prevYear + "年1-6月"));
					}
				} else {
					result.add(new DateBranchItem(prevYear + "01", prevYear + "年1-6月"));
				}
			}
		}
		return result;
	}

	public static List<DateBranchItem> getQuarterDateBranchItems(Date startDate) {
		List<DateBranchItem> result = new ArrayList<DateBranchItem>();
		Date endDate = new Date();
		Calendar cale = Calendar.getInstance();
		cale.setTime(endDate);
		int quarter = cale.get(Calendar.MONTH) / 3 + 1;
		String year = DateFormatUtils.format(endDate, "yyyy");
		for (int i = quarter; i > 0; i--) {
			int month = i * 3;
			if (month == 3) {
				result.add(new DateBranchItem(year + "03", year + "年1-3月"));
			} else if (month == 6) {
				result.add(new DateBranchItem(year + "06", year + "年4-6月"));
			} else if (month == 9) {
				result.add(new DateBranchItem(year + "09", year + "年7-9月"));
			} else if (month == 12) {
				result.add(new DateBranchItem(year + "12", year + "年10-12月"));
			}
		}

		if (startDate != null) {
			int yearNum = DateTools.differenceYear(startDate, endDate);
			for (int i = 1; i <= yearNum; i++) {
				Date temp = DateUtils.addYears(endDate, -i);
				String prevYear = DateFormatUtils.format(temp, "yyyy");
				if (i == yearNum) {
					cale.setTime(startDate);
					quarter = cale.get(Calendar.MONTH) / 3 + 1;
					for (int j = quarter; j > 0; j--) {
						int month = j * 3;
						if (month == 3) {
							result.add(new DateBranchItem(prevYear + "03", prevYear + "年1-3月"));
						} else if (month == 6) {
							result.add(new DateBranchItem(prevYear + "06", prevYear + "年4-6月"));
						} else if (month == 9) {
							result.add(new DateBranchItem(prevYear + "09", prevYear + "年7-9月"));
						} else if (month == 12) {
							result.add(new DateBranchItem(prevYear + "12", prevYear + "年10-12月"));
						}
					}
				} else {
					result.add(new DateBranchItem(prevYear + "12", prevYear + "年10-12月"));
					result.add(new DateBranchItem(prevYear + "09", prevYear + "年7-9月"));
					result.add(new DateBranchItem(prevYear + "06", prevYear + "年4-6月"));
					result.add(new DateBranchItem(prevYear + "03", prevYear + "年1-3月"));
				}
			}
		}

		return result;
	}

	public static List<DateBranchItem> getMonthDateBranchItems(Date startDate) {
		List<DateBranchItem> result = new ArrayList<DateBranchItem>();
		Date endDate = new Date();
		int monthNum = DateTools.differenceMonth(startDate, endDate);
		for (int i = monthNum; i >=0 ; i--) {
			Date prevDate = DateUtils.addMonths(startDate, i);
			String year = DateFormatUtils.format(prevDate, "yyyy");
			String month = DateFormatUtils.format(prevDate, "MM");
			result.add(new DateBranchItem(year + month, year + "年" + month + "月"));
		}
		return result;
	}

	public static List<DateBranchItem> getYearDateBranchItems(Date startDate) {
		List<DateBranchItem> result = new ArrayList<DateBranchItem>();
		Date endDate = new Date();
		int yearNum = DateTools.differenceYear(startDate, endDate);
		for (int i = yearNum; i >= 0; i--) {
			Date prevDate = DateUtils.addYears(startDate, i);
			String year = DateFormatUtils.format(prevDate, "yyyy");
			result.add(new DateBranchItem(year, year + "年"));
		}
		return result;
	}

	public static DateBranchItem getPrevHalfYearDateBranchItem(Date currentDate, Date endDate, int prevNum) {
		double temp = 0.0;
		if (prevNum >= 2) {
			temp = temp + (prevNum / 2.0);
		}
		if (prevNum % 2 > 0) {
			temp = temp + 0.5;
		}

		Calendar cale = Calendar.getInstance();
		cale.setTime(currentDate);
		int halfYear = cale.get(Calendar.MONTH) / 6 + 1;
		if (halfYear == 1) {
			temp = temp + 0.5;
		}
		Date prevDate = currentDate;
		if (temp >= 1) {
			prevDate = DateUtils.addYears(currentDate, -(int) temp);
		}
		if (endDate != null) {
			int diffYear = DateTools.differenceYear(endDate, prevDate);
			if (diffYear < 0) {
				return null;
			} else if (diffYear == 0) {
				//同年
				Calendar endCale = Calendar.getInstance();
				endCale.setTime(endDate);
				int endHalfYear = endCale.get(Calendar.MONTH) / 6 + 1;
				if (endHalfYear > halfYear) {
					//下半年 - 上半年
					return null;
				}
			}
		}
		if (temp % 1 > 0) {
			String prevYear = DateFormatUtils.format(prevDate, "yyyy");
			return new DateBranchItem(prevYear + "01", prevYear + "年1-6月");
		} else {
			String prevYear = DateFormatUtils.format(prevDate, "yyyy");
			return new DateBranchItem(prevYear + "07", prevYear + "年7-12月");
		}
	}

	public static String getTableSuffix(DateBranchTableType type, Date currentDate) {
		if (DateBranchTableType.year.equals(type)) {
			String year = DateFormatUtils.format(currentDate, "yyyy");
			return "_" + year;
		} else if (DateBranchTableType.halfYear.equals(type)) {
			String year = DateFormatUtils.format(currentDate, "yyyy");
			Calendar cale = Calendar.getInstance();
			cale.setTime(currentDate);
			int halfYear = cale.get(Calendar.MONTH) / 6 + 1;
			return "_" + year + "0" + halfYear;
		} else if (DateBranchTableType.quarter.equals(type)) {
			String year = DateFormatUtils.format(currentDate, "yyyy");
			Calendar cale = Calendar.getInstance();
			cale.setTime(currentDate);
			int quarter = cale.get(Calendar.MONTH) / 3 + 1;
			return "_" + year + "0" + quarter;
		} else if (DateBranchTableType.month.equals(type)) {
			String month = DateFormatUtils.format(currentDate, "yyyyMM");
			return "_" + month;
		}
		return "";
	}

	public static Date getDateByItemId(String id) {
		try {
			//必须多的在前，少的在后
			return DateUtils.parseDate(id, "yyyyMM","yyyy");
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
		return null;
	}

	public static class DateBranchItem {

		private String id;
		private String name;
		private Date date;

		public DateBranchItem() {
		}

		public DateBranchItem(String id, String name) {
			this.id = id;
			this.name = name;
		}

		public String getId() {
			return id;
		}

		public void setId(String id) {
			this.id = id;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public Date getDate() {
			if (this.date == null) {
				try {
					this.date = getDateByItemId(this.id);
				} catch (Exception e) {
					log.error(e.getMessage(), e);
				}
			}
			return date;
		}

	}
}
